package interceptor;

import cache.ListConfigCache;
import dao.mapper.FuncDisplayFieldMapper;
import dao.mapper.FuncDisplayInfoMapper;
import model.dto.ListConfigDTO;
import model.dto.ListInfoDTO;
import model.entity.FuncDisplayField;
import model.entity.FuncDisplayFieldExample;
import model.entity.FuncDisplayInfo;
import model.entity.FuncDisplayInfoExample;
import org.dozer.Mapper;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: Administrator
 * @Date: 2019/2/24/024 11 01
 * @Description: 系统初始化拦截切面
 */
public class SystemInitInspect implements InitializingBean {
    @Autowired
    FuncDisplayInfoMapper funcDisplayInfoMapper;
    @Autowired
    FuncDisplayFieldMapper funcDisplayFieldMapper;

    @Autowired
    Mapper mapper;

    @Override
    public void afterPropertiesSet() {
        FuncDisplayInfoExample funcDisplayInfoExample = new FuncDisplayInfoExample();
        FuncDisplayFieldExample funcDisplayFieldExample = new FuncDisplayFieldExample();
        List<FuncDisplayInfo> funcDisplayInfos = funcDisplayInfoMapper.selectByExample(funcDisplayInfoExample);
        List<FuncDisplayField> funcDisplayFields = funcDisplayFieldMapper.selectByExample(funcDisplayFieldExample);
        funcDisplayInfos.forEach(funcDisplayInfo -> {
            Integer funcId = funcDisplayInfo.getFuncId();
            List<ListConfigDTO> fieldConfigList = funcDisplayFields.stream().filter(funcDisplayField -> funcDisplayField.getFuncId().equals(funcId)).map(funcDisplayField ->
                    mapper.map(funcDisplayField, ListConfigDTO.class)
            ).collect(Collectors.toList());
            ListConfigCache.addConfigMap(funcId.toString(), fieldConfigList);
            ListConfigCache.addInfoMap(funcId.toString(), mapper.map(funcDisplayInfo, ListInfoDTO.class));
        });
    }
}
